package com.company.cloud.platform.modules.mgr.controller;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.modules.api.request.*;
import com.company.cloud.user.modules.api.response.PermissionModel;
import com.company.cloud.user.modules.api.response.PermissionQueryResp;
import com.company.cloud.user.modules.api.response.RoleModel;
import com.company.cloud.user.modules.api.response.UserBaseResp;
import com.company.cloud.user.modules.api.service.PermissionPlatformRemoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Authc
@RestController
@RequestMapping("/admin/mgr/biz/permission")
@Api(tags="Admin_MGR_APP权限(菜单)管理")
public class PermissionBizMgrController {


    @Autowired
    private PermissionPlatformRemoteService permissionPlatformRemoteService;


    @PostMapping("/tree")
    @ApiOperation(value="查询", response=PermissionModel.class)
    public BaseResult tree() {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        PermissionQueryRequest request=new PermissionQueryRequest();
        BaseClientResult<PermissionQueryResp> permissionQueryResp=permissionPlatformRemoteService.queryPermission(request);
        PermissionQueryResp data=  BaseClientResult.getResultData2Error(permissionQueryResp);
        return BaseResult.ok(data.getItems());
    }


    @PostMapping("/add")
    @ApiOperation(value="新增", response=PermissionModel.class)
    @StaffLogc(title = "新增APP权限(菜单)", action = "新增APP权限(菜单)", actionAdo = "APP权限(菜单)")
    public BaseResult add(@RequestBody PermissionAddRequest params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<PermissionModel> permissionResult= permissionPlatformRemoteService.addPermission(params);
        PermissionModel data= BaseClientResult.getResultData2Error(permissionResult);
        return BaseResult.ok(data);
    }

    @PostMapping("/update")
    @ApiOperation(value="修改", response=PermissionModel.class)
    @StaffLogc(title = "修改APP权限(菜单)", action = "修改APP权限(菜单)", actionAdo = "APP权限(菜单)")
    public BaseResult update(@RequestBody PermissionUpdateRequest params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<PermissionModel> permissionResult= permissionPlatformRemoteService.updatePermission(params);
        PermissionModel data=BaseClientResult.getResultData2Error(permissionResult);
        return BaseResult.ok(data);
    }


    @GetMapping("/get")
    @ApiOperation(value="获取", response= PermissionModel.class)
    public BaseResult get(@RequestParam("code")String code) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        PermissionGet2DelRequest params=new PermissionGet2DelRequest();
        params.setCode(code);
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        BaseClientResult<PermissionModel> permissionResult= permissionPlatformRemoteService.infoPermission(params);
        PermissionModel data=BaseClientResult.getResultData2Error(permissionResult);
        return BaseResult.ok(data);
    }


    @GetMapping("/del")
    @ApiOperation(value="删除", response= BaseResult.class)
    @StaffLogc(title = "删除APP权限(菜单)", action = "删除APP权限(菜单)", actionAdo = "APP权限(菜单)")
    public BaseResult del(@RequestParam("code")String code) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        PermissionGet2DelRequest params=new PermissionGet2DelRequest();
        params.setCode(code);
        params.setOpIp(userInfo.getIp());
        params.setOpUserId(userInfo.getId());
        permissionPlatformRemoteService.delPermission(params);
        return BaseResult.ok();
    }


}
